package 算法.leetcode.offer;

/**
 * @author lchenglong
 * @date 2022/3/21
 */
public class Offer63 {

    public static void main(String[] args) {
        Offer63 instance = new Offer63();
        int[] prices = new int[]{7,1,5,3,6,4};
        int ans = instance.maxProfit2(prices);
        System.out.println(ans);
    }
    public int maxProfit(int[] prices) {
        if (prices == null||prices.length==0)return 0;
        int pre = 0,res = 0;
        for (int j=1;j<prices.length;j++){
            int diff = prices[j]-prices[j-1];
            pre = Math.max(pre+diff,0);
            res = Math.max(pre,res);
        }
        return res;
    }

    public int maxProfit2(int[] prices){
        int minPrice = Integer.MAX_VALUE;
        int maxProfit = 0;
        for (int i = 0; i < prices.length; i++) {
            // 股票最低点
            if (prices[i] < minPrice) {
                minPrice = prices[i];
                // 选择一个最高点
            } else if (prices[i] - minPrice > maxProfit) {
                maxProfit = prices[i] - minPrice;
            }
        }
        return maxProfit;
    }
}
